#include <cstdio>
#include <cstdlib>
#include <math.h>
// Included files for OpenGL Rendering
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif

#include "boundingbox.h"
#include "vertex.h"
#include "mesh.h"
// ====================================================================
// ====================================================================

void BoundingBox::paint() const {
  // draw a wireframe box to represent the boundingbox
  glColor3f(1.0, 0, 0);
  glLineWidth(1);
  glDisable(GL_LIGHTING);
  glBegin(GL_LINES);

  glVertex3f(minimum.x(),minimum.y(),minimum.z());
  glVertex3f(maximum.x(),minimum.y(),minimum.z());
  glVertex3f(minimum.x(),minimum.y(),minimum.z());
  glVertex3f(minimum.x(),maximum.y(),minimum.z());
  glVertex3f(maximum.x(),maximum.y(),minimum.z());
  glVertex3f(maximum.x(),minimum.y(),minimum.z());
  glVertex3f(maximum.x(),maximum.y(),minimum.z());
  glVertex3f(minimum.x(),maximum.y(),minimum.z());

  glVertex3f(minimum.x(),minimum.y(),minimum.z());
  glVertex3f(minimum.x(),minimum.y(),maximum.z());
  glVertex3f(minimum.x(),maximum.y(),minimum.z());
  glVertex3f(minimum.x(),maximum.y(),maximum.z());
  glVertex3f(maximum.x(),minimum.y(),minimum.z());
  glVertex3f(maximum.x(),minimum.y(),maximum.z());
  glVertex3f(maximum.x(),maximum.y(),minimum.z());
  glVertex3f(maximum.x(),maximum.y(),maximum.z());

  glVertex3f(minimum.x(),minimum.y(),maximum.z());
  glVertex3f(maximum.x(),minimum.y(),maximum.z());
  glVertex3f(minimum.x(),minimum.y(),maximum.z());
  glVertex3f(minimum.x(),maximum.y(),maximum.z());
  glVertex3f(maximum.x(),maximum.y(),maximum.z());
  glVertex3f(maximum.x(),minimum.y(),maximum.z());
  glVertex3f(maximum.x(),maximum.y(),maximum.z());
  glVertex3f(minimum.x(),maximum.y(),maximum.z());

  glEnd();
  glEnable(GL_LIGHTING);	   
}

//Find distance from the upper left corner to (front face) to the lower right
//corner (back face) of the cuboid. 
double BoundingBox::maxLength() const{
  double distance;
  distance = sqrt( ((maximum.x()-minimum.x())*(maximum.x()-minimum.x()))+
		((maximum.y()-minimum.y())*(maximum.y()-minimum.y()))+
		((maximum.z()-minimum.z())*(maximum.z()-minimum.z())) );
  return distance/2.0;
}	

//Similar to maxLength(). Except it's finding the longest distance from
//the center of the cuboid to a vertex of the object. 
double BoundingBox::smallerRadius(Mesh *aMesh) const{
  double x, y, z, distance;
  double longest = 0.0;
  Vec3f center;
  getCenter(center);
  for (int i = 0; i < aMesh->numVertices(); ++i) {  
    x = aMesh->getVertex(i)->x();
    y = aMesh->getVertex(i)->y();
    z = aMesh->getVertex(i)->z();
    distance = sqrt( ( (center.x()-x)*(center.x()-x) )+
		( (center.y()-y)*(center.y()-y) )+
		( (center.z()-z)*(center.z()-z) ) );
    if(distance > longest)
      longest = distance;
  }
  return longest;
}

// ====================================================================
// ====================================================================
